def partition_list(mylist, num):
    if not mylist or num is None:
        raise Exception('非法输入')
    head, tail = mylist, mylist
    node = mylist
    while node:
        # 缓存next
        nextnode = node.next
        # 头部插入
        if node.val < num:
            node.next = head
            head = node
        # 尾部插入
        else:
            tail.next = node
            tail = node
        node = nextnode
    # 清除tail的下一个
    tail.next = None
    return head


from utils import list12345, print_list, list9

ans = partition_list(list12345, 4)
print_list(ans)

ans2 = partition_list(list9, 1)
print_list(ans2)
